window.endPoint="vmuss07.deri.ie:8182/ld4s";
function basicSearch() {
	//
	var basicSearchData = {
		"BasicSearchDiv" : {
			"type" : "div",
			"style" : "Field-content",
			"content" : {
				"BasicSearchlabel" : {
					"type" : "label",
					"message" : "",
					"style" : "label-message"
				},
				"Sensor" : {
					"label" : "Sensor:",
					"type" : "uri",
					"isData" : "true",
					"defaultValue" : "spitfire-project.eu:8182/ld4s"
				},
				"uri" : {
					"label" : "endpoint:",
					"type" : "uri",
					"isData" : "true",
					"defaultValue" : window.endPoint
				},
			/*
			 * "resourceID" : { "label" : "resourceID:", "type" : "input",
			 * "isData" : "true", "defaultValue" : "" }
			 */

			}

		}
	};

	createHTML(basicSearchData, $("#basicSearch"));
}

function AdvanceSearch() {

	var AdvancePart = {
		"AdvanceSearchDiv" : {
			"type" : "div",
			"style" : "Field-content",
			"content" : {
				"AdvanceLabel" : {
					"type" : "label",
					"message" : "Advance search:",
					"style" : "label-message"
				},
				"AdvanceChoice" : {
					"type" : "radio",
					"label" : "Search Type:",
					"children" : {
						"BasicSearchType" : {
							"label" : "Basic Search",
							"value" : "bs",
							"checked" : "true",
							"click" : function() {
								$("#AdvanceDetailDiv").html("");
							}
						},
						"ovType" : {
							"label" : "Observation Value",
							"value" : "ov",
							"checked" : "false",
							"click" : function() {
								generateCode("ov");
							}
						},
						"deviceType" : {
							"label" : " Device, Sensing Device, Sensor",
							"value" : "device",
							"checked" : "false",
							"click" : function() {
								generateCode("device");
							}
						},
						"tpsType" : {
							"label" : "Sensor Temporal Property",
							"value" : "tps",
							"checked" : "false",
							"click" : function() {
								generateCode("tps");
							}
						},
						"platformType" : {
							"label" : "Platform, Testbed",
							"value" : "platform",
							"checked" : "false",
							"click" : function() {
								generateCode("platform");
							}
						},
						"tppType" : {
							"label" : "Platform Temporal Property",
							"value" : "tpp",
							"checked" : "false",
							"click" : function() {
								generateCode("tpp");
							}
						},
						"meas_capabType" : {
							"label" : " Measurement Capability",
							"value" : "meas_capab",
							"checked" : "false",
							"click" : function() {
								generateCode("meas_capab");
							}
						},
						"meas_propType" : {
							"label" : "Measurement Property",
							"value" : "meas_prop",
							"checked" : "false",
							"click" : function() {
								generateCode("meas_prop");
							}
						},
						"data_linkType" : {
							"label" : "data link",
							"value" : "data_link",
							"checked" : "false",
							"click" : function() {
								generateCode("data_link");
							}
						},
						"link_reviewType" : {
							"label" : "link review",
							"value" : "link_review",
							"checked" : "false",
							"click" : function() {
								generateCode("link_review");
							}
						}

					}
				}
			}
		}
	};

	createHTML(AdvancePart, $("#specialSearch"));

	$("#AdvanceSearchDiv").append("<div id='AdvanceDetailDiv'></div>");

}

function generateCode(arg) {
	
	var content = AppData[arg].page.view.Basic.content;
	// delete the data type and store div
	delete content.storeDiv;

	// delete the resource ID
	delete content.resourceId;
	

	if (arg == "link_review") {
		delete content.vote;
		
		delete content.comment;
		 
	}
	$("#AdvanceDetailDiv").html("");
	createHTML(content, $("#AdvanceDetailDiv"));
	commonInformation();
}

function commonInformation() {
	$("#commonSearch").html("");
	var content = $.extend({}, sharedView.content);
	content.storeDataLabel = {};
	content.storeDiv = {};

	// delete the data we do not need
	delete content.sharedViewContent.content.contextbutton;
	delete content.sharedViewContent.content.context;
	// delete the send function the serve
	delete content.sharedViewContent.content.commonChoiceDiv.content.SendingURI;
	// delete description
	delete content.sharedViewContent.content.commonChoiceDiv.content.description;

	createHTML(content.sharedViewContent.content, $("#commonSearch"));

}
function NotNull(obj) {
	return (obj != undefined && obj != null && obj != "");
}

function Addcommonline(commonData) {

	var str = "";
	if (NotNull(commonData.sendData.base_datetime)) {
		str += "\n?x clf:bt \"" + commonData.sendData.base_datetime + "\"^^xsd:dateTime .";
	}

	if (NotNull(commonData.sendData.archive)) {
		str += "\n?x spt:tsMap <" + commonData.sendData.archive + "> .";
	}
	if (commonData.sendData.author != undefined) {
		str += "\n?x prov:wasGeneratedBy ?y .";
		if (NotNull(commonData.sendData.author[0].email)) {
			str += "\n?y foaf:mbox \"" + commonData.sendData.author[0].email + "\" .";
		}

		if (NotNull(commonData.sendData.author[0].firstname)) {
			str += "\n?y foaf:name \"" + commonData.sendData.author[0].firstname + "\" .";
		}
		if (NotNull(commonData.sendData.author[0].surname)) {
			str += "\n?y foaf:surname \"" + commonData.sendData.author[0].surname + "\" .";
		}
		if (NotNull(commonData.sendData.author[0].nickname)) {
			str += "\n?y foaf:nick \"" + commonData.sendData.author[0].nickname + "\" .";
		}
		if (NotNull(commonData.sendData.author[0].weblog)) {
			str += "\n?y foaf:weblog \"" + commonData.sendData.author[0].weblog + "\" .";
		}
	}
	return str;
}

function ov_node(SendData, commonData) {
	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://xmlns.com/foaf/0.1/>" + "\nSELECT *" + "{ GRAPH <"+window.endPoint+"/graph/ov>{";

	if (NotNull(SendData.values)) {
		$.each(SendData.values, function() {
			str += "\n?x spt:value \"" + this + "\"^^xsd:double .";
		});
	}

	if (NotNull(SendData.start_range)) {
		str += "\n?x spt:tStart \"" + SendData.start_range + "\" .";
	}

	if (NotNull(SendData.end_range)) {
		str += "\n?x spt:tEnd \"" + SendData.end_range + "\" .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "\n}}";
	return str;
}

function device_node(SendData, commonData) {
	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nSELECT *{ " + "\nGRAPH <"+window.endPoint+"/graph/device>  {\n";

	if (NotNull(SendData.tsproperties)) {
		$.each(SendData.tsproperties, function() {
			str += "\n?x spt:temporal <" + this + "> .";
		});
	}

	if (NotNull(SendData.observation_values)) {
		$.each(SendData.observation_values, function() {
			str += "\n?x spt:out <" + this + "> .";
		});
	}

	if (NotNull(SendData.platform)) {
		str += "\n?x ssn:onPlatform <" + SendData.platform + "> .";
	}

	if (NotNull(SendData.observed_property)) {
		str += "\n?x spt:obs <" + SendData.observed_property + "> .";
	}

	if (NotNull(SendData.uom)) {
		//str += "\n?x spt:uom <" + SendData.uom + "> .";
		str +="\n?x spt:uom "+SendData.uom+"^^xsd:string .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function tps_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nPREFIX dul:     <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl/>"
			+ "\nPREFIX wsg:     <http://www.w3.org/2003/01/geo/wgs84_pos#lat>" + "\nSELECT *" + "\n{ GRAPH <"+window.endPoint+"/graph/tps>   {";

	if (NotNull(SendData.net_links)) {
		$.each(SendData.net_links, function() {
			str += "\n ?x spt:netLink <" + this + "> .";
		});
	}

	if (NotNull(SendData.net_role)) {
		str += "\n?x spt:netRole <" + SendData.net_role + "> .";
	}

	if (NotNull(SendData.foi)) {
		str += "\n?x ssn:featureOfInterest <" + SendData.foi + "> .";
	}

	if (NotNull(SendData.start_range)) {
		str += "\n?x spt:tStart \"" + SendData.start_range + "\"^^xsd:dateTime .";
	}

	if (NotNull(SendData.end_range)) {
		str += "\n?x spt:tEnd \"" + SendData.end_range + "\"^^xsd:dateTime .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	if (NotNull(SendData.location_name)) {
		str += "\n?x dul:hasLocation \"" + SendData.location_name + "\" .";
	}

	if (NotNull(SendData.location_coords)) {
		var s = SendData.location_coords.split('_');

		str += "\n?x wsg:lat \"" + s[0] + "\"^^xsd:double .";
		str += "\n?x wsg:long \"" + s[1] + "\"^^xsd:double .";
	}
	str += Addcommonline(commonData);
	str += "}}";
	return str;
}
function platform_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nPREFIX dul:     <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl/>"
			+ "\nPREFIX wsg:     <http://www.w3.org/2003/01/geo/wgs84_pos#lat>" + "\nSELECT *" + "\n{ GRAPH <"+window.endPoint+"/graph/platform>   {";

	if (NotNull(SendData.tpproperties)) {
		$.each(SendData.tpproperties, function() {
			str += "\n?x spt:temporal <" + this + "> .";
		});
	}

	if (NotNull(SendData.feeds)) {
		$.each(SendData.feeds, function() {
			str += "\n?x spt:feed <" + this + "> .";
		});
	}

	if (NotNull(SendData.status)) {
		str += "\n?x spt:status <" + SendData.status + "> .";
	}

	if (NotNull(SendData.foi)) {
		str += "\n?x ssn:featureOfInterest <" + SendData.foi + "> .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function tpp_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nPREFIX dul:     <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl/>"
			+ "\nPREFIX wsg:     <http://www.w3.org/2003/01/geo/wgs84_pos#lat>" + "\nSELECT *" + "\n{ GRAPH  <"+window.endPoint+"/graph/tpp>   {";

	if (NotNull(SendData.systems)) {
		$.each(SendData.systems, function() {
			str += "\n?x ssn:attachedSystem <" + this + "> .";
		});
	}

	if (NotNull(SendData.algorithms)) {
		$.each(SendData.algorithms, function() {
			str += "\n?x ssn:implements <" + this + "> .";
		});
	}

	if (NotNull(SendData.plarform_id)) {
		str += "\n?x spt:forSensor <" + SendData.plarform_id + "> .   ";
	}

	if (NotNull(SendData.deployment)) {
		str += "\n?x ssn:hasDeployment <" + SendData.deployment + "> .";
	}

	if (NotNull(SendData.start_range)) {
		str += "\n?x spt:tStart \"" + SendData.start_range + "\"^^xsd:dateTime .";
	}

	if (NotNull(SendData.end_range)) {
		str += "\n?x spt:tEnd \"" + SendData.end_range + "\"^^xsd:dateTime .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	if (NotNull(SendData.location_name)) {
		str += "\n?x dul:hasLocation \"" + SendData.location_name + "\" .";
	}

	if (NotNull(SendData.location_coords)) {
		var s = SendData.location_coords.split('_');

		str += "\n?x wsg:lat \"" + s[0] + "\"^^xsd:double .";
		str += "\n?x wsg:long \"" + s[1] + "\"^^xsd:double .";
	}

	if (NotNull(SendData.owners)) {
		str += "\n?x spt:ownedBy <http://spitfire-project.eu:8182/ld4s/resource/people/" + SendData.owners[0][0].firstname + "> .";
	}

	if (NotNull(SendData.worn_by)) {
		str += "\n?x spt:wornBy <http://spitfire-project.eu:8182/ld4s/resource/people/" + SendData.worn_by[0][0].firstname + "> .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function meas_capab_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nSELECT *" + "\n{ GRAPH <"+window.endPoint+"/graph/meas_capab> {";

	if (NotNull(SendData.measurement_properties)) {
		$.each(SendData.measurement_properties, function() {
			str += "\n?x ssn:hasMeasurementProperty <" + this + "> .";
		});
	}

	if (NotNull(SendData.observed_property)) {
		str += "\n?x ssn:forProperty <" + SendData.observed_property + "> .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}
function meas_prop_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nPREFIX owl:     <http://www.w3.org/2002/07/owl#>" + "\nSELECT *"
			+ "\n{ GRAPH <"+window.endPoint+"/graph/meas_prop>  {";

	if (NotNull(SendData.uom)) {
		//str += "\n?x spt:uom <" + SendData.uom + "> .";
		str +="\n?x spt:uom "+SendData.uom+"^^xsd:string .";
	}

	if (NotNull(SendData.predicate)) {
		str += "\n?x owl:onProperty <" + SendData.predicate + "> .";
	}

	if (NotNull(SendData.MaxValue)) {
		str += "\n?x spt:hasMaxValue <" + SendData.MaxValue + "> .";
	}

	if (NotNull(SendData.MinValue)) {
		str += "\n?x spt:hasMinValue <" + SendData.MinValue + "> .";
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	if (NotNull(SendData.conditions)) {
		str += "\n?x ssn:inCondition ?y .";

		if (NotNull(SendData.conditions[0][0].oncondition_uom)) {
			str += "\n?y spt:uom <" + SendData.conditions[0][0].oncondition_uom + "> .";
		}

		if (NotNull(SendData.conditions[0][0].oncondition_predicate)) {
			str += "\n?y owl:onProperty <" + SendData.conditions[0][0].oncondition_predicate + "> .";
		}

		if (NotNull(SendData.conditions[0][0].oncondition_property)) {
			str += "\n?y ssn:forProperty <" + SendData.conditions[0][0].oncondition_property + "> .";
		}
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function data_link_node(SendData, commonData) {

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>"
			+ "\nPREFIX prov:    <http://www.w3.org/ns/prov#>" + "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX ssn:     <http://purl.oclc.org/NET/ssnx/ssn#>" + "\nPREFIX dc:      <http://purl.org/dc/terms/>" + "\nPREFIX rev:     <http://purl.org/stuff/rev#>"
			+ "\nSELECT *{ GRAPH <"+window.endPoint+"/graph/general>  {";

	if (NotNull(SendData.to)) {
		str += "\n?x spt:to <" + SendData.to + "> .";
	}

	if (NotNull(SendData.title)) {
		str += "\n?x spt:title \"" + SendData.title + "\"^^xsd:string .";
	}

	if (NotNull(SendData.updated)) {
		str += "\n?x dc:temporal \"" + SendData.updated + "\"^^xsd:dateTime .";
	}

	if (NotNull(SendData.feedbacks)) {
		$.each(SendData.feedbacks, function() {
			str += "\n?x rev:hasFeedback <" + this + "> .";
		});
	}

	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function link_review_node(SendData, commonData) {
	

	var str = "PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\nPREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>" + "\nPREFIX spt:     <http://spitfire-project.eu/ontology/ns/>" + "\nPREFIX clf:     <http://purl.org/NET/corelf#>" + "\nPREFIX prov:    <http://www.w3.org/ns/prov#>"
			+ "\nPREFIX foaf:    <http://http://xmlns.com/foaf/0.1/>" + "\nPREFIX rev:     <http://purl.org/stuff/rev#>" + "\nSELECT *" + "\n{ GRAPH <"+window.endPoint+"/graph/general>   {";

	
	if (NotNull(SendData.link)) {
		str += "\n?x rev:isFeedbackOf <"+SendData.link+"> .";
	}
		
	if (NotNull(SendData.type)) {
		str += "\n?x rdf:type spt:" + SendData.type + " .";
	}

	str += Addcommonline(commonData);
	str += "}}";
	return str;
}

function searchButton() {
	var searchButton = {
		"searchButton" : {
			"type" : "button",
			"style" : "generateButton",
			"label" : "",
			"value" : "Search",
			"click" : function() {
				SendData = GetAjaxData();
				commonData = GetCommonData();
				var type = $("#AdvanceChoice").attr("value");

				if (type == "bs" || type == "" || type == undefined) {
					var url = $("#Sensor").val();
					$.ajax({
						url : "http://" + url,
						type : "get",
						crossDomain : true,
						contentType : "application/json; charset=utf-8",
						dataType : "html",
						headers : {
							"Accept" : "application/rdf+xml"
						},
						success : function(data) {
							$("#result").html("");
							$("#result").codeArea(data);
						},
						error : function() {
							$("#result").html("");
							$("#result").codeArea("url or server error");
						}
					});
				} else {
					SendData = GetAjaxData();
					commonData = GetCommonData();
					window.endPoint=SendData.uri;
					
					var data = eval(type + "_node(SendData,commonData)");
					var send=new Object();
					send.query=new Array();
					send.query[0]=encodeURIComponent(data);
					
					//var sendingData=SendData.uri+encodeURIComponent(data);
					
					$("#result").html("");
					$("#result").append("<b>sending query is :</b>");
					$("#result").codeArea(data);
					
					$.ajax({
						url:SendData.uri+"/sparql",
						type : "post",
						//crossDomain : true,
						contentType : "application/json; charset=utf-8",
						dataType : "html",
						data:JSON.stringify(send) ,
						headers : {
							"Accept" : "application/rdf+xml"
						},
						success:function(data){
							$("#result").append("<b>search result is :</b>");
							$("#result").codeArea(data);
						},error:function(){
							$("#result").append("<b>query error or no result,please check the query again </b>");
						}
					});

					
				}

			}

		}
	};
	createHTML(searchButton, $("#searchButtonDiv"));
}

$(function() {
	basicSearch();
	AdvanceSearch();
	commonInformation();
	searchButton();
});